package com.xlongwei.accounts.dao;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import org.noear.solon.Solon;
import org.noear.solon.annotation.Around;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.data.annotation.Cache;
import org.noear.solon.data.annotation.CacheRemove;
import org.noear.solon.data.annotation.Tran;

import com.xlongwei.accounts.config.MySqlUtils;
import com.xlongwei.accounts.dao.VipDao.VipDs;
import com.xlongwei.accounts.model.Vip;

@VipDs
@Component
public class VipDao {
        public static final String dsName = Solon.cfg().get("vip.ds", "accounts");
        @Inject
        MySqlUtils mySqlUtils;
        // t_vip存的是mobile_ds，数据变更时直接通过tags移除所有缓存，不必在Vip计算getMobile值精确移除缓存
        @Cache(key = "vip.mobile.${mobile}", tags = "vips")
        public List<Vip> findByMobile(Long mobile) {
                List<Vip> list = mySqlUtils.sql("select id,mobile_ds,reg_day,vip_day,partner,inviter from t_vip where mobile_ds like ?",
                                mobile + "%").queryRowList(Vip.class);
                return list != null ? list : Collections.emptyList();
        }

        public List<Vip> findByVipDay(LocalDate vipDay) {
                List<Vip> list = mySqlUtils.sql("select id,mobile_ds,reg_day,vip_day,partner,inviter from t_vip where vip_day < ? order by vip_day desc",
                                vipDay).queryRowList(Vip.class);
                return list != null ? list : Collections.emptyList();
        }

        @Tran
        @CacheRemove(tags = "vips")
        public Integer create(Vip row) {
                Object key = mySqlUtils.sql("insert into t_vip(mobile_ds,reg_day,vip_day,partner,inviter) values(?,?,?,?,?)",
                                row.getMobileDs(), row.getRegDay(), row.getVipDay(),
                                row.getPartner(), row.getInviter()).updateReturnKey();
                return Integer.parseInt(key.toString());
        }

        @Tran
        @CacheRemove(tags = "vips")
        public Integer update(Vip row) {
                return mySqlUtils.sql("update t_vip set mobile_ds=?,reg_day=?,vip_day=?,partner=?,inviter=? where id=?",
                                row.getMobileDs(), row.getRegDay(), row.getVipDay(),
                                row.getPartner(), row.getInviter(), row.getId()).update();
        }

        @Target({ ElementType.METHOD, ElementType.TYPE })
        @Retention(RetentionPolicy.RUNTIME)
        @Around(VipDsInterceptor.class)
        public @interface VipDs {
        }
        
        public static class VipDsInterceptor implements Interceptor {

                @Override
                public Object doIntercept(Invocation inv) throws Throwable {
                        // VipDao只存取vip.ds数据源，不随用户登录的数据源而改变
                        try {
                                MySqlUtils.dsName.set(dsName);
                                return inv.invoke();
                        } finally {
                                MySqlUtils.dsName.remove();
                        }
                }

        }
}
